/*
 * Copyright 2010 - 2011, The PLDesktop Development Team
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

#include "processor.h"

#include <machine/machine.h>
#include <kerneldebugconsole.h>
#include <bootoutput.h>

PLProcessor::PLProcessor() {
}

void PLProcessor::start() {
	PLKernelDebugConsole::instance().printf("\nCreate and set the Interrupt Description table ... ");
	m_interruptManager.initialiseProcessor();
	PLKernelDebugConsole::instance().printf("done");
	m_processorStates = new ProcessorState[PLMachine::instance().processorCount()];
	m_processor.start();
	asm volatile("mov %%cr0, %0" : "=r"(m_controlRegister0));
	asm volatile("mov %%cr4, %0" : "=r"(m_controlRegister4));
	PLKernelDebugConsole::instance().printf("\n   Control Register 0 value: 0x%x", m_controlRegister0);
	PLKernelDebugConsole::instance().printf("\n   Control Register 4 value: 0x%x", m_controlRegister4);
}

void PLProcessor::start1() {
	m_processor.start1();
}

void PLProcessor::setControlRegister0(PLProcessor::ControlRegister0 flag) {
	m_controlRegister0 |= flag;
	PLKernelDebugConsole::instance().printf("\n   Set new Control Register 0 value: 0x%x", m_controlRegister0);
	asm volatile("mov %0, %%cr0" :: "r"(m_controlRegister0));
}

uint32_t PLProcessor::controlRegister0Value() {
	asm volatile("mov %%cr0, %0" : "=r"(m_controlRegister0));
	return m_controlRegister0;
}

uint32_t PLProcessor::controlRegister2Value() {
	uint32_t controlRegister2;
	asm volatile("mov %%cr2, %0" : "=r"(controlRegister2));
	return controlRegister2;
}

uint32_t PLProcessor::controlRegister3Value() {
	uint32_t controlRegister3;
	asm volatile("mov %%cr3, %0" : "=r"(controlRegister3));
	return controlRegister3;
}

void PLProcessor::setControlRegister4(PLProcessor::ControlRegister4 flag) {
	m_controlRegister4 |= flag;
	PLKernelDebugConsole::instance().printf("\n   Set new Control Register 4 value: 0x%x", m_controlRegister4);
	asm volatile("mov %0, %%cr4" :: "r"(m_controlRegister4));
}

uint32_t PLProcessor::controlRegister4Value() {
	asm volatile("mov %%cr4, %0" : "=r"(m_controlRegister4));
	return m_controlRegister4;
}

ProcessorState &PLProcessor::processorState(size_t processor) {
	return m_processorStates[processor];
}

PLInterruptManager &PLProcessor::interruptManager() {
	return m_interruptManager;
}

void PLProcessor::setInterrupts(bool enabled) {
	if(enabled) {
		asm volatile("sti");
	} else {
		asm volatile("cli");
	}
}

bool PLProcessor::interrupts() {
	size_t result;
	asm volatile("pushf\n"
					 "pop %0\n"
					 "and $0x200, %0\n" :
					 "=r" (result));
	return (result != 0);
}

void PLProcessor::halt() {
	if(interrupts()) {
		setInterrupts(false);
	}
	asm volatile("hlt\n");
}
